/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myetl;

/**
 *
 * @author apuangr1
 */
public class ImportManager {

    String fileName;
    ConnManager conn;

    ImportManager(String filename, ConnManager conn) {
        this.conn = conn;
        this.fileName = filename;
    }

    public void run() {
        //
        System.out.println("------------------------- " + this.fileName);
        AppManager apm = new AppManager();
        apm.init(this.fileName);
        apm.setDestConn(conn.getDestConn());
        apm.setSourceConn(conn.getSourceConn());
        SourceData sd = new SourceData(apm);
        DestData dd = new DestData(apm);
        DatabaseAgent inserter = new DatabaseAgent(apm);
        Transformer tf = new Transformer(apm);
        System.out.println("------------------------start tf");
        tf.listFunction();
//        System.out.println(tf.convertToDate("change_date", 1091010).toString());
//        System.exit(0);
//        inserter.buildInsertStatement();
        dd.listColumn();
        int count = 1;
        sd.retrieve();
        while (sd.moveNext()) {
            count++;
            for (int i = 1; i <= sd.columnCount; i++) {
                if (sd.getColumnTypeNmae(i).toUpperCase().contains("CHAR")) {
                    String sData = sd.getString(i);
                    System.out.println("source:" + i + ":" + sData);
                    if (sData.length() == 0) {
                        dd.setValue(i, "");
                    } else {
                        dd.setValue(i, tf.convertToString(dd.getColumnName(i), ThaiUtilities.ASCII2Unicode(sData)));
                    }

                }
                if (Utility.isDecimalColumn(sd.getColumnTypeNmae(i))) {

                    if (dd.getColumnType(i).contains("DATE")) {
                        System.out.println("source:" + i + ":decimal TO daTE:" + sd.getDecimal(i));
                        dd.setValue(i, tf.convertToDate(dd.getColumnName(i), sd.getDecimal(i)));

                    } else {
                        if (dd.getColumnType(i).contains("CHAR")) {
                            System.out.println("source:" + i + ":decimal TO char :" + sd.getDecimal(i));
                            dd.setValue(i, tf.convertToString(dd.getColumnName(i), sd.getDecimal(i)));
                        } else {
                            System.out.println("source:" + i + ":decimal:" + sd.getDecimal(i));
                            dd.setValue(i, sd.getDecimal(i));
                        }
                    }
                }
                if (sd.getColumnTypeNmae(i).contains("INT")) {
                    System.out.println("source:" + i + ":int:" + sd.getInteger(i));
                    dd.setValue(i, sd.getInteger(i));
                }
                if (sd.getColumnTypeNmae(i).contains("DATE")) {
                    System.out.println("source:" + i + ":" + sd.getDate(i));
                    dd.setValue(i, sd.getDate(i));
                }
            }
            dd.listData();
            inserter.addData(dd);

        }
        inserter.updateDB();

//        
        int row = 1;

//        System.exit(0);
    }
}
